<?php
namespace app\manager\controller;

use think\Db;
use app\common\traits\Provide;

class Home extends Base
{
    use Provide;

    public function homeList()
    {
        if(request()->isAjax())
        {
            $list = Db::name('room')
                ->alias('r')
                ->join('teacher t','r.teacher_id = t.id')
                ->field('r.*, t.name as teacher_name')
                ->select();
            return responseJson(0,'success', $list);
        }else{
            return $this->fetch('home_list');
        }
    }

    /**
     * 添加房间
     */
    public function addHome()
    {
        if(request()->isAjax())
        {
            $data = input('post.');
            unset($data['file']);
            $data['power_role'] = implode(',' ,array_keys($data['power_role']));
            $data['c_time'] = time();
            $result = Db::name('room')->insert($data);
            if($result)
            {
                return responseJson(200,'添加成功');
            }else{
                return responseJson(400,'添加失败');
            }
        }else{
            $data['teacher'] = Db::name('teacher')->where('state',1)->select();
            $data['role'] = Db::name('role')->where('state',1)->select();
            $this->assign('data', $data);
            return $this->fetch('add_home');
        }
    }

    /*
     * 修改房间
     */
    public function editHome()
    {
        $id = input('id');
        if(request()->isAjax())
        {
            $data = input('post.');
            unset($data['file']);
            $data['power_role'] = implode(',' ,array_keys($data['power_role']));
            $data['u_time'] = time();
            $result = Db::name('room')->where('id', $id)->update($data);
            if($result)
            {
                //删除Redis缓存里的值
                $room_name = 'room_info_'.$data['room_id'];
                self::redis()->del($room_name);
                $this->getRoomInfo($data['room_id']);
                return responseJson(200,'修改成功');
            }else{
                return responseJson(400,'修改失败');
            }
        }else{
            $data['teacher'] = Db::name('teacher')->where('state',1)->select();
            $data['role'] = Db::name('role')->where('state',1)->select();
            $data['info'] = Db::name('room')->where('id', $id)->find();
            $data['info']['power_role'] = explode(',' ,$data['info']['power_role']);
            $this->assign('data', $data);
            return $this->fetch('edit_home');
        }
    }

    /*
     * 删除房间
     */
    public function delHome()
    {
        $room_id = input('room_id');
        $result = Db::name('room')->where('room_id', $room_id)->delete();
        $room_name = 'room_info_'.$room_id;
        self::redis()->del($room_name);
        if(!$result)
        {
            return responseJson(400,'删除失败');
        }
        return responseJson(200,'修改成功');
    }

    /*
     * banner
     */
    public function homeBanner()
    {
        if(request()->isAjax())
        {
            $list = Db::name('banner')
                ->select();
            return responseJson(0,'success', $list);
        }else{
            return $this->fetch('banner_list');
        }
    }

    public function addBanner()
    {
        if(request()->isAjax())
        {
            $data = input('post.');
            unset($data['file']);
            $data['power_role'] = implode(',' ,array_keys($data['power_role']));
            $data['c_time'] = time();
            $result = Db::name('banner')->insert($data);
            if($result)
            {
                return responseJson(200,'添加成功');
            }else{
                return responseJson(400,'添加失败');
            }
        }else{
            $room_list = Db::name('room')->where('room_state', 1)->column('room_id');
            $this->assign('data', $room_list);
            return $this->fetch('add_banner');
        }
    }
}